<template>
  <div class="wrapper">
    <div class="login-box">
      <div class="logo">
        <img
          src="../../assets/logo.png"
          alt=""
          class="logo-img"
        />
        <b class="logo-text">
          人力资源管理系统
        </b>
      </div>
      <el-form :rules="rules" :model="staff" ref="loginForm">
        <el-form-item prop="code">
          <el-input
            size="medium"
            placeholder="请输入账号"
            class="input-field"
            prefix-icon="el-icon-user"
            v-model.trim="staff.code"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            size="medium"
            placeholder="请输入密码"
            class="input-field"
            prefix-icon="el-icon-lock"
            show-password
            v-model.trim="staff.password"
          ></el-input>
        </el-form-item>
        <div class="login-button-container">
          <el-form-item>
            <el-button type="primary" size="medium" class="login-button" @click="handleLogin">登 录</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="footer">
      <a class="team-link" href="https://gitee.com/ultravioletrays/renliziyuanguanlixitong" target="_blank">杨骅麟团队呈现</a>
    </div>
  </div>
</template>

<script>
import {login} from '../../api/login'

export default {
  name: 'Login',
  data() {
    return {
      staff: {},
      // 效验规则
      rules: {
        code: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          login(this.staff).then(
            response => {
              if (response.code === 200) {
                localStorage.setItem("staff", JSON.stringify(response.data)) // 存储用户信息到浏览器
                localStorage.setItem("token", response.token) // 存储token
                this.$message.success("登录成功！")
                this.$router.push({
                  path: '/home'
                })
              } else {
                this.$message.error(response.message)
              }
            }
          )
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
.wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-image: url('../../assets/images/background.jpg');
  overflow: hidden;
  background-size: cover;
}

.login-box {
  background-color: #fff;
  width: 350px;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.logo-img {
  width: 30px;
  margin-right: 10px;
}

.logo-text {
  color: #303133;
  font-weight: bold;
  font-size: 24px;
}

.input-field {
  margin: 10px 0;
}

.login-button-container {
  margin: 20px 0;
}

.login-button {
  width: 100%;
}

.footer {
  margin-top: 20px;
  text-align: center;
}

.team-link {
  font-size: 18px;
  color: #ffffff;
  font-weight: bold;
  text-decoration: none;
  padding: 10px 15px;
  background-color: #3f51b5;
  border-radius: 5px;
  transition: color 0.3s, background-color 0.3s;
}

.team-link:hover {
  color: #3f51b5;
  background-color: #ffffff;
}
</style>
